package com.ydlclass.serialization.json;

import com.alibaba.fastjson2.JSON;
import com.ydlclass.serialization.Serializer;
import lombok.extern.slf4j.Slf4j;


/**
 * 使用fastjson2进行序列化和反序列化
 * @author it楠老师
 * @createTime 2023-03-09
 */
@Slf4j
public class JsonSerializer implements Serializer {
    @Override
    public byte[] serialize(Object obj) {
        if(log.isDebugEnabled()){
            log.debug("Serialization is being done using json.");
        }
        return JSON.toJSONBytes(obj);
    }

    @Override
    public <T> T deserialize(byte[] bytes, Class<T> clazz) {
        if(log.isDebugEnabled()){
            log.debug("Deserialization is being done using json.");
        }
        return (T)JSON.parseObject(bytes, clazz);
    }

    @Override
    public byte getCode() {
        return 2;
    }
}
